/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: ModelProbServiceImpl.java
 * Author:   13075787
 * Date:     2016年7月7日 上午10:16:53
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.service.impl.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.model.ModelConstant;
import com.suning.sawp.intf.model.ModelMainListBean;
import com.suning.sawp.intf.model.ModelProbOprReqBean;
import com.suning.sawp.intf.model.ModelProbReason;
import com.suning.sawp.intf.model.ModelProbVo;
import com.suning.sawp.intf.model.ModelZoneInfo;

/**
 * 样机管理<br>
 * 样机异常
 *
 * @author 13075787
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("modelProbService")
public class ModelProbServiceImpl {

    @Autowired
    ModelProbDaoServiceImpl modelProbDaoService;

    @Autowired
    ModelCommClusterServiceImpl modelCommClusterService;

    @Autowired
    ModelBlendServiceImpl modelBlendService;

    /**
     * 功能描述: <br>
     * 样机异常原因列表
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public ReturnMsg<List<ModelProbReason>> queryModelProbReason() {
        ReturnMsg<List<ModelProbReason>> result = new ReturnMsg<List<ModelProbReason>>();
        List<ModelProbReason> resultList = new ArrayList<ModelProbReason>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.putAll(ModelConstant.PRORB_REASON_INFOS);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            ModelProbReason modelProrbReason = new ModelProbReason();
            modelProrbReason.setReasonCode(entry.getKey());
            modelProrbReason.setReasonStr((String) entry.getValue());
            resultList.add(modelProrbReason);
        }
        result.setData(resultList);
        return result;
    }

    /**
     * 功能描述: <br>
     * 未处理异常
     */
    public ReturnMsg<List<ModelProbVo>> queryModelProbNoHandleList(StoreManDto storeMan, Integer index, Integer size) {
        ReturnMsg<List<ModelProbVo>> result = new ReturnMsg<List<ModelProbVo>>();
        result.setData(modelProbDaoService.queryModelProbNoHandleList(storeMan.getStaffId(), index, size));
        return result;
    }

    /**
     * 功能描述: <br>
     * 未审核异常
     */
    public ReturnMsg<List<ModelProbVo>> queryModelProbNoAuditList(StoreManDto storeMan, Integer index, Integer size) {
        ReturnMsg<List<ModelProbVo>> result = new ReturnMsg<List<ModelProbVo>>();
        result.setData(modelProbDaoService.queryModelProbNoAuditList(storeMan.getStaffId(), index, size));
        return result;
    }

    /**
     * 功能描述: <br>
     * 异常处理/异常审核
     */
    @SuppressWarnings("rawtypes")
    public ReturnMsg modelProbHandle(StoreManDto storeMan, Integer status, List<ModelProbOprReqBean> probOprReqList) {
        ReturnMsg result = new ReturnMsg();
        if (null != status && CollectionUtils.isNotEmpty(probOprReqList)
                && (status.equals(ModelConstant.PROBLEM_OPR_STATUS_DONE)
                        || status.equals(ModelConstant.PROBLEM_OPR_STATUS_OVER)
                        || status.equals(ModelConstant.PROBLEM_OPR_STATUS_ING))) {
            // 审核通过,将该异常处理更新样机库
            if (status.equals(ModelConstant.PROBLEM_OPR_STATUS_OVER)) {
                // 需要变更的样机清单数据
                List<ModelMainListBean> mainList = new ArrayList<ModelMainListBean>();
                for (int i = 0; i < probOprReqList.size(); i++) {
                    ModelProbOprReqBean probOprReq = probOprReqList.get(i);
                    String reasonCode = probOprReq.getReasonCode();
                    String commCode = probOprReq.getCommCode();
                    // 判断空，商品编码与异常原因不为空
                    if (StringUtils.isBlank(commCode)
                            || StringUtils.isBlank(ModelConstant.getProbReasonValue(reasonCode))
                            || null == probOprReq.getCommNum()) {
                        result.setError(ModelConstant.MODEL_ERROR_NO_PARAM,
                                ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_NO_PARAM));
                        return result;
                    }
                    // 真实出样商品编码
                    String locActualCommCode = probOprReq.getActualCommCode();
                    // 维护簇商品信息
                    if (StringUtils.isNotBlank(locActualCommCode) && StringUtils.isNotBlank(commCode)
                            && !commCode.equals(locActualCommCode)) {
                        ModelZoneInfo modelZone = new ModelZoneInfo();
                        modelZone.setDisCode(storeMan.getRegionCode());
                        modelZone.setDisName(storeMan.getRegionName());
                        modelZone.setOrgCode(storeMan.getBranchCode());
                        modelZone.setOrgName(storeMan.getBranchName());
//                        modelCommClusterService.addModelCommCluster(modelZone, locActualCommCode, commCode);
                        // 插入真实出样商品->入库样机清单
                        modelBlendService.addCommMain(storeMan.getStoreCode(), locActualCommCode);
                    }
                    // 盘点数量
                    Integer dealCommNum = probOprReq.getCommNum();
                    Integer commNum = modelProbDaoService.getProbFinalCommNum(dealCommNum, reasonCode);
                    if (null == commNum) {
                        // 查询指定异常
                        ModelProbVo dbProb = modelProbDaoService.getModelProbVo(storeMan.getStoreCode(), commCode);
                        commNum = null != dbProb ? dbProb.getCheckCommNum() : ModelConstant.NUM_ZERO;
                    }
                    // 要修改的样机清单数据
                    ModelMainListBean mainModel = new ModelMainListBean();
                    mainModel.setStoreCode(storeMan.getStoreCode());
                    mainModel.setCommCode(commCode);
                    mainModel.setCommNum(commNum);
                    mainList.add(mainModel);
                }
                // 更新（删除）样机清单中的数据
                modelBlendService.updateOrDelModelCommNum(mainList);
            }
            if (modelProbDaoService.updateModelProbOpr(storeMan.getStoreCode(), storeMan.getStaffId(), status,
                    probOprReqList)) {
                result.setRetFlag(ReturnMsg.SUCCESS);
            } else {
                result.setRetFlag(ReturnMsg.FAIL);
            }
        } else {
            result.setError(ModelConstant.MODEL_ERROR_NO_PARAM,
                    ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_NO_PARAM));
        }
        return result;
    }

}
